#include <sstream>
#include <iostream>
#include "Player.h"
#include "../Logger/Logger.h"


/*void Player::openCardBySeat() const
{
  connector_->openCard(seat_)
}*/

Player::Player(): connector_(0), seat_(0), name_(""), settings_(Settings::getInstance())
{
}

Player::Player(Connector &connector): connector_(&connector), seat_(0), name_(""),
               settings_(Settings::getInstance())
{
}

int Player::openCard() const
{
  return connector_->openCard(seat_);
}

int Player::getTotem() const
{
  return connector_->getTotem(seat_);
}

int Player::connect(const std::string &name, const time_t &connectTime, int &error)
{
  int seat = connector_->connect(name, connectTime, error);
  log(seat,DEBUG);
  if (error == 0)
  {
    seat_ = seat;
    name_ = name;
  }
  return error;
}

void Player::setName(const std::string &name)
{
  name_ = name;
}

const std::string& Player::getName() const
{
  return name_;
}

void Player::setSeat(int seat)
{
  seat_ = seat;
}

int Player::getSeat() const
{
  return seat_;
}

int Player::setConnector(Connector &connector)
{
  if(connector_ != 0)
  {
    return 1;
  }
  connector_ = &connector;
  log("connector ok");
  return 0;
}

Connector* Player::getConnector() const
{
  return connector_;
}



int Player::checkKey(char value, int place) const
{
  log("START CHECKING!");
  std::stringstream seatStr;
  seatStr << place;
  std::string keyOpen = "PLAYER" + seatStr.str() +"_KEY_OPEN";
  std::string keyGetTotem = "PLAYER" + seatStr.str() +"_KEY_GETTOTEM";
  std::string valueString = settings_.get(keyOpen);

  std::stringstream valueStr;
  valueStr << value;
  log("char is");
  log(valueStr.str());
  log(valueString);
  if (valueString == valueStr.str())
  {
   // openCard();
    return 1;
  }
  valueString = settings_.get(keyGetTotem);
  if (valueString == valueStr.str())
  {
   // connector_->getTotem(seat_);
    return 2;
  }

  return 0;
}

bool Player::checkKey(char value) const
{
  log("START CHECKING!");
  std::stringstream seatStr;
  seatStr << seat_;
  std::string keyOpen = "PLAYER" + seatStr.str() +"_KEY_OPEN";
  std::string keyGetTotem = "PLAYER" + seatStr.str() +"_KEY_GETTOTEM";
  std::string valueString = settings_.get(keyOpen);

  std::stringstream valueStr;
  valueStr << value;
  log("char is");
  log(valueStr.str());
  log(valueString);
  if (valueString == valueStr.str())
  {
    openCard();
    return true;
  }
  valueString = settings_.get(keyGetTotem);
  if (valueString == valueStr.str())
  {
    connector_->getTotem(seat_);
    return true;
  }

  return false;
}
